#!/bin/bash
#
# Logging
exec > >(tee -a /var/log/ce-mgm-install.log)
exec 2> >(tee -a /var/log/ce-mgm-install.log >&2)
#
# Script variables
VERSION=`cat version`
TOTAL_ROOT_SIZE=`df -h / | tail -1 | awk {'print $2'} | sed 's/.$//'`
ROOT_SIZE_FREE=`df -h / | tail -1 | awk {'print $4'} | sed 's/.$//'`
MEMORY_INSTALLED=`vmstat -s | head -1 | cut -d K -f1 | awk {'print $1'}`
MEMORY_CONVERSION=1024
MEMORY_INSTALLED_MB=`echo $((MEMORY_INSTALLED / MEMORY_CONVERSION))`
HTTP_CONNECT=`curl -Is www.google.com:80 | head -1`
#
# Begin Installation
clear
echo "cloudextent Installation"
echo "------------------------"
echo "Installing cloudextent $VERSION on `hostname`."
echo " "
#
# System Checks
echo "We will now check to see if this system is ready for install..."
echo " "
echo "Checking the root partition size..."
if [[ $TOTAL_ROOT_SIZE -ge 30 ]]
then
	echo "The size of the root partition is $TOTAL_ROOT_SIZE GB."
else
	echo "The root parition is only $TOTAL_ROOT_SIZE GB, please increase / to 30 GB minimum."
	echo " "
	exit 0;
fi
echo " "
echo "Checking root partition's free space..."
if [[ $ROOT_SIZE_FREE -ge 20 ]]
then
        echo "The root parition has $ROOT_SIZE_FREE GB free."
else
        echo "The root parition only has $ROOT_SIZE_FREE GB free, please increase free space on / to 20 GB minimum."
        echo " "
        exit 0;
fi
echo " "
echo "Checking the size of physical memory..."
if [[ $MEMORY_INSTALLED -ge 1000000 ]]
then
        echo "There is $MEMORY_INSTALLED_MB MB installed."
else
        echo "There is only $MEMORY_INSTALLED_MB MB free, please increase the system memory to 2GB minimum."
        echo " "
        exit 0;
fi
echo " "
echo "Checking internet connectivity..."
if [[ $HTTP_CONNECT == *"OK"* ]]
then
	echo "The internet connection is working!"
else
	echo "No internet connection, exiting..."
	exit 0;
fi
echo " "
echo "The following packages and their dependencies will be installed:"
echo " "
echo "php php-cli php-common httpd apr apr-util httpd httpd-tools openssl openssl-libs mod_ssl"
echo " "
read -p "Press the [Enter] key to continue or control-c to quit..."
echo " "
yum install -y php php-cli php-common apr apr-util httpd httpd-tools openssl openssl-libs mod_ssl
echo " "
echo "Verifying package installation..."
if [[ `rpm -qa php` == *"php"* ]]
then
        echo "`rpm -qa php` is installed."
else
        echo "No php package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa php-cli` == *"php-cli"* ]]
then
        echo "`rpm -qa php-cli` is installed."
else
        echo "No php-cli package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa php-common` == *"php-common"* ]]
then
        echo "`rpm -qa php-common` is installed."
else
        echo "No php-common package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa httpd` == *"httpd"* ]]
then
        echo "`rpm -qa httpd` is installed."
else
        echo "No httpd package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa httpd-tools` == *"httpd-tools"* ]]
then
        echo "`rpm -qa httpd-tools` is installed."
else
        echo "No httpd-tools package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa apr` == *"apr"* ]]
then
        echo "`rpm -qa apr` is installed."
else
        echo "No apr package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa apr-util` == *"apr-util"* ]]
then
        echo "`rpm -qa apr-util` is installed."
else
        echo "No apr-util package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa openssl` == *"openssl"* ]]
then
        echo "`rpm -qa openssl` is installed."
else
        echo "No openssl package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa openssl-libs` == *"openssl-libs"* ]]
then
        echo "`rpm -qa openssl-libs` is installed."
else
        echo "No openssl-libs package was installed, exiting..."
        exit 0;
fi
echo " "
if [[ `rpm -qa mod_ssl` == *"mod_ssl"* ]]
then
        echo "`rpm -qa mod_ssl` is installed."
else
        echo "No mod_ssl package was installed, exiting..."
        exit 0;
fi
echo " "
echo "Configuring apache with the following settings..."
echo " "
echo "We will backup existing files..."
echo "cp -pr /var/www/html /var/www/html.\$\$"
echo "cp -pr /etc/pki/tls/certs /etc/pki/tls/certs.\$\$"
echo "cp -pr /etc/pki/tls/private /etc/pki/tls/private.\$\$"
echo " "
echo "We will remove old apache files..."
echo "rm -rf  /var/www/html/*"
echo "We will copy new files into place..."
echo " "
echo "cp etc/httpd/conf.d/default.conf /etc/httpd/conf.d/default.conf"
echo " "
echo "Dummy keys..."
echo "cp etc/pki/tls/certs/ca.crt /etc/pki/tls/certs/ca.crt"
echo "cp etc/pki/tls/private/ca.key /etc/pki/tls/private/ca.key"
echo " "
echo "We will copy the management software into place..."
echo "cp -r ce-mgm/* /var/www/html/"
echo " "
echo "We will set the following permissions..."
echo "chmod 644 /etc/httpd/conf.d/default.conf /etc/pki/tls/certs/ca.crt /etc/pki/tls/private/ca.key"
echo "chmod 755 /var/www/html"
echo "chown root:root /etc/httpd/conf.d/default.conf /etc/pki/tls/certs/ca.crt /etc/pki/tls/private/ca.key /var/www/html"
echo " "
read -p "Press the [Enter] key to continue or control-c to quit..."
echo " "
echo "Moving configuration into place..."
echo "Backuping existing files..."
cp -pr /var/www/html /var/www/html.$$
cp -pr /etc/pki/tls/certs /etc/pki/tls/certs.$$
cp -pr /etc/pki/tls/private /etc/pki/tls/private.$$
echo " "
echo "Copyng new files into place..."
cp etc/httpd/conf.d/default.conf /etc/httpd/conf.d/default.conf
echo " "
echo "Removing old apache files..."
rm -rf  /var/www/html/*
echo " "
echo "Copying dummy keys..."
cp etc/pki/tls/certs/ca.crt /etc/pki/tls/certs/ca.crt
cp etc/pki/tls/private/ca.key /etc/pki/tls/private/ca.key
echo " "
echo "Copying the management software into place..."
cp -r ce-mgm/* /var/www/html/
echo " "
echo "Setting permissions..."
chmod 644 /etc/httpd/conf.d/default.conf /etc/pki/tls/certs/ca.crt /etc/pki/tls/private/ca.key
chmod 755 /var/www/html
chown root:root /etc/httpd/conf.d/default.conf /etc/pki/tls/certs/ca.crt /etc/pki/tls/private/ca.key /var/www/html
echo " "
echo "Restarting apache..."
/bin/systemctl restart  httpd.service
echo " "
echo "Installation complete..."
echo " "
